package com.pic.mgt.monitor;

import java.io.File;
import java.io.IOException;

import com.pic.mgt.log.IgxLog;
import com.pic.mgt.monitor.parser.FormatParser;
import com.pic.mgt.unit.Unit;

public abstract class MessageUnitFactory implements UnitFactory {
	private FormatParser messageTypeParser;
	private FormatParser messageBusinessTypeParser;
	
	protected String TRANSOK_MESSAGE;
	
	@Override
	public Unit createUnit(File confirmationFile)
									throws IOException,
									   	   WrongUnitFormatException,
									   	   RedundantConfirmationFileException {
		Unit messageUnit = null;
		
		try {
			// 取得訊息類別 (ex: Trans OK 或 Trans NG 之類的 ...)
			// throws WrongUnitFormatException
			String messageTypeString = (String) this.messageTypeParser
					.parse(confirmationFile.getName());
			// 取得訊息業務類別(ex:一般配信,指示配信... ; 即時集信...)
			// throws WrongUnitFormatException
			String businessTypeString = (String)this.messageBusinessTypeParser.
					parse(confirmationFile.getName());
			
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isInfoMode()) {
				IgxLog.messageDetailsLog
						.info(this, "Creating message unit for confirmation file '{}'.",
								confirmationFile.getName());
			}
			// ********************** Log **********************
			
			// throws IOException, WrongUnitFormatException
			messageUnit = createIGXTypeUnit(confirmationFile, messageTypeString, businessTypeString);

			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isInfoMode()) {
				IgxLog.messageDetailsLog
						.info(this, "Creating message unit for confirmation file '{}' completed.",
								confirmationFile.getName());
			}
			// ********************** Log **********************
			
		} catch (WrongUnitFormatException e1) {
			// ********************** Log **********************
			IgxLog.messageDetailsLog.error(this, e1.getMessage());
			// ********************** Log **********************
			
			throw e1;
		} catch (IOException e2) {
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog.debug(this, e2.getMessage());
			}
			// ********************** Log **********************
			
			throw e2;
		}
		
		return messageUnit;
	}
	
	protected abstract Unit createIGXTypeUnit(File confirmationFile,
											  String messageTypeString,
											  String messageIGXTypeString)
						throws IOException, WrongUnitFormatException, RedundantConfirmationFileException; 
	

	// --- getter and setter
	
	public void setMessageTypeParser(FormatParser messageTypeParser) {
		this.messageTypeParser = messageTypeParser;
	}

	public void setMessageBusinessTypeParser(FormatParser messageBusinessTypeParser) {
		this.messageBusinessTypeParser = messageBusinessTypeParser;
	}

	public void setTRANSOK_MESSAGE(String tRANSOKMESSAGE) {
		TRANSOK_MESSAGE = tRANSOKMESSAGE;
	}
}
